<template>
  <a-drawer
    title="合并房源"
    :width="400"
    :visible="visible"
    :body-style="{
      height: 'calc(100% - 115px)',
      boxSizing: 'border-box',
      overflowY: 'auto',
    }"
    :maskClosable="true"
  >
    <a-form layout="vertical">
      <a-row>
        <a-col :span="24">
          <a-alert
            show-icon
            type="warning"
            message="合并到目标房源后本房源会被删除，请谨慎操作！"
          />
        </a-col>
        <a-col :span="24" style="margin-top: 20px">
          <a-form-item label="合并到房间" v-bind="validateInfos.merge_room_id">
            <a-select
              v-model:value="modelRef.merge_room_id"
              placeholder="请选择目标房源"
            >
              <a-select-option
                v-for="item of allRoomList"
                :key="item.id"
                :value="item.id"
              >
                <span>{{ item.room_name }}</span>
                <span
                  style="color: rgba(0, 0, 0, 0.5); margin-left: 10px"
                  v-if="item.room_status_txt"
                >
                  （{{ item.room_status_txt }}）
                </span>
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="是否合并房源面积">
            <template #label>
              <span>是否合并房源面积</span>
              <text-tip tips="开启后，被合并的房源会增加合并房源的面积" />
            </template>
            <a-switch
              v-model:checked="modelRef.merge_area"
              :checkedValue="1"
              :unCheckedValue="0"
              checked-children="开"
              un-checked-children="关"
            ></a-switch>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <div class="drawer-footer">
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">提交</a-button>
      </a-space>
    </div>
  </a-drawer>
</template>

<script>
import { defineComponent, reactive, toRefs, watch } from "vue";
import { Form, message } from "ant-design-vue";
import request from "../../../../common/utils/request";
import { TextTip } from "bl-common-vue3";

const useForm = Form.useForm;

export default defineComponent({
  name: "MergeRoom",
  components: { TextTip },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    detail: {
      type: Object,
      default: () => ({}),
    },
  },
  setup(props, { emit }) {
    const state = reactive({
      allRoomList: [],
    });

    /** 表单 */
    const modelRef = reactive({
      merge_room_id: undefined,
      merge_area: 1,
    });
    const required = [{ required: true, message: `必填项` }];
    const rulesRef = reactive({
      merge_room_id: required,
    });

    const { resetFields, validate, validateInfos } = useForm(
      modelRef,
      rulesRef
    );

    /** 提交 */
    const onSubmit = () => {
      validate().then(() => {
        const params = {
          ...modelRef,
          village_id: props.detail.village_id,
          id: props.detail.id,
          needMsg: true,
        };
        request.post("/village", "/project/room/merge", params).then((res) => {
          message.success(res.msg || "操作成功！");
          emit("success");
        });
      });
    };

    const getAllRoomList = () => {
      request
        .get("/village", "/project/room/all", {
          village_id: props.detail.village_id,
          layer_id: props.detail.layer_id,
        })
        .then((res) => {
          state.allRoomList = res.list.filter((it) => it.id != props.detail.id);
        });
    };

    watch(
      () => props.visible,
      (val) => {
        if (val) {
          getAllRoomList();
        } else {
          resetFields();
        }
      }
    );

    /** 关闭弹窗 */
    const onClose = () => {
      emit("close");
    };

    return {
      modelRef,
      validateInfos,
      ...toRefs(state),
      onClose,
      onSubmit,
    };
  },
});
</script>
